package com.sanqing.dao;

import com.sanqing.po.Recruitment;

import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Restrictions;

/**
 * A data access object (DAO) providing persistence and search support for
 * Recruitment entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.sanqing.po.Recruitment
 * @author MyEclipse Persistence Tools
 */

public class RecruitmentDao extends BaseHibernateDao {
	private static final Log log = LogFactory.getLog(RecruitmentDao.class);
	// property constants
	public static final String TITLE = "title";
	public static final String POSITION = "position";
	public static final String QUALIFICATION = "qualification";
	public static final String SKILL = "skill";
	public static final String NUM = "num";
	public static final String STATUS = "status";

	public void save(Recruitment transientInstance) {
		log.debug("saving Recruitment instance");
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.save(transientInstance);
			tx.commit();
			closeSession();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public void update(Recruitment persistentInstance) {
		log.debug("deleting Recruitment instance");
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.update(persistentInstance);
			tx.commit();
			closeSession();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public void delete(Recruitment persistentInstance) {
		log.debug("deleting Recruitment instance");
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.delete(persistentInstance);
			tx.commit();
			closeSession();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Recruitment findById(java.lang.Integer id) {
		log.debug("getting Recruitment instance with id: " + id);
		try {
			Recruitment instance = (Recruitment) getSession().get(
					"com.sanqing.po.Recruitment", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Recruitment instance) {
		log.debug("finding Recruitment instance by example");
		try {
			List results = getSession().createCriteria(
					"com.sanqing.po.Recruitment").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Recruitment instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Recruitment as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	public List findByPosition(Object position) {
		return findByProperty(POSITION, position);
	}

	public List findByQualification(Object qualification) {
		return findByProperty(QUALIFICATION, qualification);
	}

	public List findBySkill(Object skill) {
		return findByProperty(SKILL, skill);
	}

	public List findByNum(Object num) {
		return findByProperty(NUM, num);
	}

	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List findAll() {
		log.debug("finding all Recruitment instances");
		try {
			String queryString = "from Recruitment";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List search(Integer id, String title, Integer deptId, Integer positionId,
			Boolean status){
		Criteria criteria = getSession().createCriteria("com.sanqing.po.Recruitment");
		if(id!=null){
			criteria.add(Restrictions.eq("id", id));
		}
		if(title!=null&&!title.equals("")){
			criteria.add(Restrictions.like("title", "%"+title+"%"));
		}
		if(status!=null){
			criteria.add(Restrictions.eq("status", status));
		}
		if(deptId!=null&&deptId>0){
			criteria.createAlias("department", "dept").add(Restrictions.like("dept.id", deptId));
		}
		if(positionId!=null&&positionId>0){
			criteria.createAlias("position", "position")
			.add(Restrictions.like("position.id", positionId));
		}
		List results = criteria.list();
		return results;
	}
	
	public Recruitment merge(Recruitment detachedInstance) {
		log.debug("merging Recruitment instance");
		try {
			Recruitment result = (Recruitment) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Recruitment instance) {
		log.debug("attaching dirty Recruitment instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Recruitment instance) {
		log.debug("attaching clean Recruitment instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}